package com.client.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.client.bean.BirdList;
import com.client.bean.BirdMessage;
import com.client.common.SpinnerProgressDialog;
import com.client.task.NoOrderBirdMessagesTask;
import com.clinet.cache.AsyncImageLoader;
import com.clinet.cache.Log;

public class BirdSearchAct extends BaseAct {
	private Context mContext = BirdSearchAct.this;
	public static AsyncImageLoader mAsyncImageLoader = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.birdsearchact);
		initBottomBar();
		init();
		onclick();
		getData();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mAsyncImageLoader.clearImageMap();
		mAsyncImageLoader.shutdownThreadPool();
	}

	private void getData() {
//		SpinnerProgressDialog.showDialog(this,"正在获取数据,请稍后...");

		mNoOrderBirdMessagesTask = new NoOrderBirdMessagesTask(mContext, callback);
		mNoOrderBirdMessagesTask.execute(mBirdList);
	}

	private NoOrderBirdMessagesTask mNoOrderBirdMessagesTask = null;

	private int[] bg = { R.drawable.gridicon, R.drawable.listicon };
	private boolean isgrid = true;

	private void onToggle() {
		if (isgrid) {
			gridl.setVisibility(View.VISIBLE);
			listl.setVisibility(View.INVISIBLE);
			toggle.setImageResource(bg[1]);
			navigatel.setVisibility(View.GONE);
			isgrid = false;
		} else {
			gridl.setVisibility(View.INVISIBLE);
			listl.setVisibility(View.VISIBLE);
			toggle.setImageResource(bg[0]);
			navigatel.setVisibility(View.VISIBLE);
			isgrid = true;
		}
	}

	private ImageView add = null;

	private void onclick() {

		add = (ImageView) findViewById(R.id.add);
		add.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				DetaiAct.MODE = DetaiAct.ADDMODE;
				Intent intent = new Intent(mContext, DetaiAct.class);
				mContext.startActivity(intent);
			}
		});
		toggle.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				onToggle();
			}

		});

		search.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mBirdList.key = searchkey.getText().toString().trim();
				mNoOrderBirdMessagesTask = new NoOrderBirdMessagesTask(mContext, callback);
				mNoOrderBirdMessagesTask.execute(mBirdList);
			}
		});
		searchkey.addTextChangedListener(new TextWatcher() {
			private CharSequence temp;

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				temp = s;
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (temp.length() > 0) {// 按关键字查
					mBirdList.key = s.toString().trim();
					mNoOrderBirdMessagesTask = new NoOrderBirdMessagesTask(mContext, callback);
					mNoOrderBirdMessagesTask.execute(mBirdList);
				} else// 全查
				{
					mBirdList.key = "";
					mNoOrderBirdMessagesTask = new NoOrderBirdMessagesTask(mContext, callback);
					mNoOrderBirdMessagesTask.execute(mBirdList);
				}
			}
		});

	}

	private TextView pagename = null;
	private TextView total = null;
	private ImageView toggle = null;
	private EditText searchkey = null;
	private ImageView search = null;
	private ListView listview = null;
	private LinearLayout gridl = null;
	private LinearLayout listl = null;
	private LinearLayout navigatel = null;

	NoOrderBirdMessagesTask.Callback callback = null;

	private void init() {
		callback = new NoOrderBirdMessagesTask.Callback() {
			@Override
			public void onComplete(final Integer result) {

				SpinnerProgressDialog.dismissDialog();
				switch (result) {
				case 1:
					// onToggle();
					initGridView();
					initListView();
					initLatterNavi();
					// pagename.setText(bean.pagename);
					total.setText(mBirdList.getTotalNum() + "");
					break;
				case 0:

					break;
				}
			}
		};
		layoutFlater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mAsyncImageLoader = new AsyncImageLoader();
		pagename = (TextView) findViewById(R.id.pagename);
		total = (TextView) findViewById(R.id.totalnum);
		toggle = (ImageView) findViewById(R.id.toggle);
		searchkey = (EditText) findViewById(R.id.searchkey);
		search = (ImageView) findViewById(R.id.search);

		gridl = (LinearLayout) findViewById(R.id.gridl);
		listl = (LinearLayout) findViewById(R.id.listl);
		navigatel = (LinearLayout) findViewById(R.id.navigatel);
		onToggle();
	}

	// -----------------------------网格----------------------S
	public void initGridView() {
		if (mBirdList == null || mBirdList.getTotalNum() <= 0) {
			return;
		}

		gridView = (GridView) findViewById(R.id.time_grid);
		mgridViewAdapter = new TimeGridAdapter();
		gridView.setAdapter(mgridViewAdapter);
		gridView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> mAdapter, View view,
					int position, long id) {
				Log.i(mBirdList.getItems().get(position).toString());
				initPopupMenu(mBirdList.getItems().get(position));
				return false;
			}
		});
	}

	private BirdList mBirdList = new BirdList();
	private GridView gridView = null;
	private TimeGridAdapter mgridViewAdapter = null;
	private LayoutInflater layoutFlater;

	public class TimeGridAdapter extends BaseAdapter {

		public TimeGridAdapter() {
			super();

		}

		public int getCount() {
			return mBirdList.getTotalNum();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final ViewHolderGridViewItem holder;
			if (convertView == null) {
				convertView = layoutFlater.inflate(
						R.layout.collection_grid_item, null);
				holder = new ViewHolderGridViewItem();
				holder.birdName = (TextView) convertView
						.findViewById(R.id.birdName);
				holder.birdImage = (ImageView) convertView
						.findViewById(R.id.birdImage);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolderGridViewItem) convertView.getTag();
			}

			BirdMessage bean = mBirdList.getItems().get(position);
			holder.birdName.setText(bean.birdName);
			String url = bean.birdImage;
			holder.birdImage.setTag(url);
			// 载入图片
			BirdSearchAct.mAsyncImageLoader.getGridViewLogoItem(mContext,
					bean.birdImage, holder.birdImage, gridView);
			return convertView;
		}
	}

	private static class ViewHolderGridViewItem {
		public TextView birdName = null;
		public ImageView birdImage = null;
	}

	// -----------------------------网格----------------------E
	// -----------------------字母导航条S
	private final static char[] LETTERS = new char[] { 'A', 'B', 'C', 'D', 'E',
			'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
			'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

	// 动态添加组件

	LinearLayout.LayoutParams lp_ff = null;

	private void initLatterNavi() {

		if (lp_ff != null) {
			return;
		}
		lp_ff = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);
		LinearLayout navigatel = (LinearLayout) findViewById(R.id.navigatel);

		LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);

		mLayoutParams.rightMargin = 1;
		mLayoutParams.leftMargin = 1;
		mLayoutParams.gravity = Gravity.CENTER_HORIZONTAL;
		mLayoutParams.weight = 1;
		for (char a : LETTERS) {
			final TextView tv = (TextView) layoutFlater.inflate(
					R.layout.latter_textview_item, null);
			// tv.setLayoutParams(lp_ff);
			tv.setText(String.valueOf(a));
			tv.setGravity(Gravity.CENTER);
			// tv.setTextAppearance(mContext, R.style.letterNavStyle);
			tv.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					String s = tv.getText().toString();
					Toast.makeText(mContext, s, Toast.LENGTH_LONG).show();
					// listview.setSelection(getStartIndex(s));
					// mListViewAdapter.notifyDataSetChanged();
					mBirdList.key = s;
					mNoOrderBirdMessagesTask = new NoOrderBirdMessagesTask(mContext, callback);
					mNoOrderBirdMessagesTask.execute(mBirdList);
				}

			});
			navigatel.addView(tv, mLayoutParams);
		}
	}

	//
	// private int getStartIndex(String letter) {
	// int returnInt = 0;
	// if (mBirdList.getTotalNum() > 0) {
	// List<BirdMessage> mlist = mBirdList.getItems();
	// for (int i = 0; i < mlist.size(); i++) {
	// if (mlist.get(i).pinyin.startsWith(letter.toLowerCase())) {
	// returnInt = i;
	// break;
	// }
	//
	// }
	// }
	// return returnInt;
	// }
	// Toast mToast=null;
	// TextView toast_textview_item =null;
	// public void showToast(String message)
	// {
	// if(mToast==null)
	// {LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
	// LinearLayout.LayoutParams.WRAP_CONTENT,
	// LinearLayout.LayoutParams.WRAP_CONTENT);
	//
	// mLayoutParams.rightMargin = 1;
	// mLayoutParams.leftMargin = 1;
	// mLayoutParams.gravity = Gravity.CENTER_HORIZONTAL;
	// mToast = new Toast(mContext);
	// mToast.setGravity(Gravity.CENTER, 150, 150);
	// mToast.setDuration(Toast.LENGTH_LONG);
	// toast_textview_item = (TextView) layoutFlater.inflate(
	// R.layout.toast_textview_item, null);
	//
	// mToast.setView(toast_textview_item);
	// }
	// toast_textview_item.setText(message);
	// mToast.show();
	// }
	// -----------------------字母导航条E
	// -----------------------------列表----------------------S
	public void initListView() {
		if (mBirdList == null || mBirdList.getTotalNum() <= 0) {
			return;
		}
		listview = (ListView) findViewById(R.id.listview);
		listview.setDivider(null);
		mListViewAdapter = new TimeListAdapter();

		listview.setAdapter(mListViewAdapter);
		listview.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> mAdapter, View view,
					int position, long id) {
				initPopupMenu(mBirdList.getItems().get(position));
				return false;
			}
		});

	}

	private TimeListAdapter mListViewAdapter = null;

	public class TimeListAdapter extends BaseAdapter {

		public TimeListAdapter() {
			super();

		}

		public int getCount() {
			return mBirdList.getTotalNum();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final ViewHolderListViewItem holder;
			if (convertView == null) {
				convertView = layoutFlater.inflate(R.layout.search_list_item,
						null);
				holder = new ViewHolderListViewItem();
				holder.birdName = (TextView) convertView
						.findViewById(R.id.list_birdName);
				holder.birdImage = (ImageView) convertView
						.findViewById(R.id.list_birdImage);
				holder.line = (ImageView) convertView.findViewById(R.id.line);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolderListViewItem) convertView.getTag();
			}

			BirdMessage bean = mBirdList.getItems().get(position);
			holder.birdName.setText(bean.birdName);
			String url = bean.birdImage;
			holder.birdImage.setTag(url);
			// 载入图片
			BirdSearchAct.mAsyncImageLoader.getListViewLogoItem(mContext,
					bean.birdImage, holder.birdImage, listview);
			return convertView;
		}
	}

	private static class ViewHolderListViewItem {
		public TextView birdName = null;
		public ImageView birdImage = null;
		public ImageView line = null;
	}

	// -----------------------------网格----------------------E

}